package UI.LessonPlan
{
	import Events.*;
	
	import LessonPlans.Tasks.Task;
	
	import mx.core.UIComponent;
	
	public class UITaskList extends UIComponent
	{
		private var taskBoxList:Vector.<UIInputBox>;
		
		public function UITaskList(taskList:Vector.<Task>)
		{
			super();
			taskBoxList = new Vector.<UIInputBox>();
			for each(var task:Task in taskList)
			{
				addTask(task);
			}
		}
		
		public function get tasks():Vector.<Task>
		{
			var taskList:Vector.<Task>=new Vector.<Task>();
			for each(var box:UIInputBox in taskBoxList)
			{
				taskList.push(new Task(box.title,box.description,box.response));
			}
			return taskList;
		}
		
		public function clear():void
		{
			for each(var box:UIInputBox in taskBoxList)
			{
				removeChild(box);
			}
			taskBoxList = new Vector.<UIInputBox>();
			positionElements();
		}
		
		public function addTask(task:Task):void
		{
			var uiBox:UIInputBox = new UIInputBox(task.name,task.description,task.response);
			taskBoxList.push(uiBox);
			addChild(uiBox);
			uiBox.addEventListener(UIInputBoxEvent.EXPAND, resizeList);
			positionElements();
		}
		
		private function resizeList(e:UIInputBoxEvent):void
		{
			dispatchEvent(new UIInputBoxEvent(e.type));
			positionElements();
		}
		
		private function positionElements():void
		{
			var yPos:Number=0;
			for each(var taskBox:UIInputBox in taskBoxList)
			{
				taskBox.y = yPos;
				yPos = taskBox.y+taskBox.height;
			}
		}
		
		public override function get height():Number
		{
			if(taskBoxList.length==0)
				return 0;
			return taskBoxList[taskBoxList.length-1].y+taskBoxList[taskBoxList.length-1].height;
		}
		
		public override function set width(w:Number):void
		{
			for each(var taskBox:UIInputBox in taskBoxList)
				taskBox.width=w;
			positionElements();
		}
	}
}